package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.crm.domain.Customer;

//1.在类上需要4个注释:父包 
@ParentPackage("json-default")  //表示父包  会使用父包定义的一些常量等
@Namespace("/")  //表示了访问路径
@Controller  //表示该action 受spring 框架控制
@Scope("prototype") //表示这个action 是多例的
public class FixedAreaAction extends BaseAction<FixedArea> {
//由于使用继承 BaseAction  故简化了代码 
	//注入  Service
	@Autowired
	private FixedAreaService fixedAreaServcie ;
	
	//保存定区操作
	@Action(value="fixedArea_save",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String save(){
		fixedAreaServcie.save((FixedArea)model);
		return SUCCESS;
	}
	
	//属性驱动  获取页面数据
	private String idStr;
	public void setIdStr(String idStr) {
		this.idStr = idStr;
	}

	//删除定区操作
	@Action(value="fixed_area_delBatch",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String delBatch(){
		
		fixedAreaServcie.delBatch(idStr);
		return SUCCESS;
	}
	
	/*分页 条件查询定区的方法:
	 * 需要两个对象 pageable  specification
	 * 1.获得pageable   :通过页面传的page  rows
	 * 2.构造条件
	 * 3.调用业务层 返回 pageData
	 * 4.将结果转成规定格式的json,压入值栈
	 * */
	@Action(value="fixedArea_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		//1.构造分页查询对象   pageable
		Pageable pageable = new PageRequest(super.page-1, rows);
		//2.构造条件查询对象  specification
		Specification specification = new Specification<FixedArea>() {
			/*构造条件查询方法,如果方法返回null,则表示无条件查询
			 * root:获取条件表达式   比如:name =?
			 * CriteriaQuery :构造简单条=条件查询  返回 提供where方法
			 * CriteriaBuilder:构造Predicate对象,条件对象 构造复杂查询效果
			 * 
			 */
			@Override
			public Predicate toPredicate(Root<FixedArea> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				//构造查询条件
				if(StringUtils.isNotBlank(model.getId())){
					//根据定区编号查询  等值
					Predicate p1 = cb.equal(root.get("id").as(String.class), model.getId());
					list.add(p1);
				}
				
				if(StringUtils.isNotBlank(model.getCompany())){
					Predicate p2 = cb.like(root.get("company").as(String.class),
							"%"+model.getCompany()+"%");
					list.add(p2);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		//调用业务层
		Page<FixedArea> pageData =  fixedAreaServcie.findPageData(specification,pageable);
		//将查询结果压栈
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}
	
	//查询未  关联定区列表的 客户信息
	@Action(value="fixedArea_findNoAssociationCustomers",results={@Result(name="success",type="json")})
	public String findNoAssociationCustomer(){
		//使用 webCilent 调用webService接口
		Collection<? extends Customer> collection = WebClient.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
		.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		//将数据结果  压入值栈
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	//查询已  关联定区列表的 客户信息
		@Action(value="fixedArea_findHasAssociationFixedAreaCustomers",results={@Result(name="success",type="json")})
	public String findHasAssociationFixedAreaCustomers() {
		// 使用 webCilent 调用webService接口
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/"
						+ model.getId()).accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);

		// 将数据结果 压入值栈
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
		
		
	//属性驱动   select提交的数据是以数组形式提交的
	private String[] customerIds;
	public void setCustomerIds(String[] customerIds) {
			this.customerIds = customerIds;
		}


	// 将客户关联到定区上
	@Action(value = "fixedArea_associationCustomersToFixedArea", 
			results = { @Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String associationCustomersToFixedArea() {
		//将ids拼接成字符串
		String customerIdStr = StringUtils.join(customerIds,",");
		// 使用 webCilent 调用webService接口
		 WebClient
				.create("http://localhost:9002/crm_management/services/customerService/associationcustomerstofixedarea?customerIdStr="
						+ customerIdStr+"&fixedAreaId="+model.getId()).put(null);
		return SUCCESS;
	}

	// 将快递员关联到定区上 注意:无效的快递员不查询,
	//属性驱动
	private Integer courierId;
	private Integer takeTimeId;

	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}
	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}
	//关联快递员  上班时间  到 定区
	@Action(value = "fixedArea_associationCourierToFixedArea", 
			results = { @Result(name = "success", type = "redirect", location ="./pages/base/fixed_area.html")})
	public String associationCourierToFixedArea(){
		fixedAreaServcie.associationCourierToFixedArea(model,courierId,takeTimeId);
		return SUCCESS;
	}
	
	
	/**
	 * 查询所有定区信息
	 * @return
	 */
	@Action(value="fixedArea_findAll",results={@Result(name="success",type="json")})
	public String findAll(){
		List<FixedArea> fixedAreas =  fixedAreaServcie.findAll();
		ActionContext.getContext().getValueStack().push(fixedAreas);
		return SUCCESS;
	}
	
}































